//
//  MNDTenderTransferedViewController.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/14.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDTenderTransferedViewController.h"
#import "MNDTenderTransferModel.h"
#import "MNDTransferedTableViewCell.h"
#import "SYRefreshControl.h"
#import "MNDTenderTransferModel.h"
#import "MNDTransferingDetailViewController.h"
@interface MNDTenderTransferedViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tenderTranssferedTableView;

//模型数组
@property (nonatomic, strong) NSMutableArray <MNDTenderTransferModel*> *tenderTransferModelArray;
//页码
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation MNDTenderTransferedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData:YES];
    
}
#pragma mark -懒加载
- (NSMutableArray<MNDTenderTransferModel *> *)tenderTransferModelArray {
    if (!_tenderTransferModelArray) {
        _tenderTransferModelArray = [[NSMutableArray alloc]init];
    }
    return _tenderTransferModelArray;
}

#pragma mark -搭建界面
- (void)setupUI {
    self.tenderTranssferedTableView.estimatedRowHeight = 100.0;
    self.tenderTranssferedTableView.rowHeight = UITableViewAutomaticDimension;
    
    WeakSelf(self);
    [SYRefreshControl configGifMJRefresh:self.tenderTranssferedTableView refreshType:MJRefreshSupportALl beginRefresh:NO timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
        
        [weakself loadData:YES];
    } loadMoreBlock:^{
        [weakself loadData:NO];
    }];
}

- (void)loadData:(BOOL)isPullDown {
    if (self.tenderTransferModelArray <= 0) {
        [self.view beginLoading];
    }
    if (isPullDown) {
        //清空模型数组
        if (self.tenderTransferModelArray.count <= 0) {
            [self.view beginLoading];
        }
        _pageNum = 1;
    } else {
        _pageNum++;
    }
    __weak typeof(self) weakSelf = self;
    
    //转让中数据获取
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"perpage": @(_pageNum), @"pagesize": @5, @"creditor_status": @-1}];
    
    [APIManager sendRequestWithPath:API_TENDER_TRANSFER_LIST parameters:params method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            
            NSArray *modelArray = [MNDTenderTransferModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"li"]];
            [SYRefreshControl configAfterLoading:weakSelf.tenderTranssferedTableView hasData:modelArray.count];
            if (isPullDown) {
                [weakSelf.view configBlankPage:SYBlankPageTypeNoData hasData:modelArray.count reloadButtonBlock:nil];
                [weakSelf.tenderTransferModelArray removeAllObjects];
            }
            [weakSelf.tenderTransferModelArray addObjectsFromArray:modelArray];
            [weakSelf.view configBlankPage:SYBlankPageTypeNoData hasData:weakSelf.tenderTransferModelArray.count reloadButtonBlock:^(id sender) {
                [weakSelf loadData:YES];
            }];
        } else {
            [SYRefreshControl configAfterLoading:weakSelf.tenderTranssferedTableView hasData:YES];
            if (isPullDown) {
                [weakSelf.view configBlankPage:SYBlankPageTypeError hasData:weakSelf.tenderTransferModelArray.count reloadButtonBlock:^(id sender) {
                    [weakSelf loadData:YES];
                }];
            } else {
                [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
            }
        }
        [weakSelf.tenderTranssferedTableView reloadData];
    } onFailure:^(NSError *error) {
        [SYRefreshControl configAfterLoading:weakSelf.tenderTranssferedTableView hasData:YES];
        
        if (isPullDown) {
            SYBlankPageType blankType = [weakSelf.view blankPageTypeWithError:error];
            [weakSelf.view configBlankPage:blankType hasData:weakSelf.tenderTransferModelArray.count reloadButtonBlock:^(id sender) {
                [weakSelf loadData:isPullDown];
            }];
        } else {
            [NetworkProgressHUD showHudNetworkError:error];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tenderTransferModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDTenderTransferModel *transferingModel = self.tenderTransferModelArray[indexPath.row];
    MNDTransferedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MNDTransferedTableViewCell"];
    cell.brrowTitleLabel.text = transferingModel.borrow_title;
    cell.borrowYearAprLabel.text = [NSString stringWithFormat:@"%@%%",transferingModel.borrow_year_apr];
    cell.creditorPrincipalLabel.text = transferingModel.creditor_principal;
    cell.creditorPriceLabel.text = transferingModel.creditor_price;
    cell.creditorPublishDateLabel.text = [NSString stringWithFormat:@"转让日期：%@",transferingModel.creditor_publish_date];
    cell.creditorDeadlineLabel.text = [NSString stringWithFormat:@"%@个月",transferingModel.creditor_deadline];
    if ([transferingModel.creditor_status isEqualToString:@"2"]) {//已撤销
        cell.statusImgView.hidden = NO;
    }else {
        cell.statusImgView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TenderTransfer" bundle:nil];
    MNDTransferingDetailViewController *transferDetailVC = [storyboard instantiateViewControllerWithIdentifier:@"MNDTransferingDetailViewController"];
    transferDetailVC.creditorId = self.tenderTransferModelArray[indexPath.row].creditor_id;
    transferDetailVC.transferStatus = @"1";
    transferDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:transferDetailVC animated:YES];
}

@end
